package com.ap.core.business.message.domain.translators.impl;

import com.ap.core.business.message.domain.translators.BindingListener;
import com.ap.core.business.message.domain.translators.BindingMetaData;
import com.ap.core.business.message.domain.translators.Translator;
import com.ap.core.cxf.jaxb.common.ActivationStatus;
import com.ap.core.cxf.jaxb.common.AddressMandatoryCountryCode;
import com.ap.core.cxf.jaxb.managemerchant.MerchantInfoType;
import com.ap.core.dao.AddressDao;
import com.ap.core.dao.DistributorDao;
import com.ap.core.dao.MerchantDao;
import com.ap.core.domain.*;
import com.ap.core.message.managemerchant.info.RequestMerchantRegistrationTypeInfo;

import java.io.ByteArrayInputStream;

/**
 * User: Ranjit.Singh
 * Implementation class for {@link com.ap.core.business.message.domain.translators.Translator} to translate MerchantRegisterType bound object to Merchant domain
 */
public class MerchantRegisterTranslator implements Translator<RequestMerchantRegistrationTypeInfo> {

    private MerchantDao merchantDao;
    private AddressDao addressDao;
    private DistributorDao distributorDao;

    @Override
    public void translate(RequestMerchantRegistrationTypeInfo boundObject, BindingMetaData metaData, BindingListener bindingListener) {
       final Distributor distributor = distributorDao.getDistributorByIdentification(boundObject.getDistributorId());
       final MerchantInfoType merchantInfoType = boundObject.getMerchant();
       final AddressMandatoryCountryCode addressMandatoryCountryCode = boundObject.getMerchant().getMerchantAddress();

       final Address merchantAddress = new Address(addressMandatoryCountryCode.getAddressLine1(),
                                                   addressMandatoryCountryCode.getAddressLine2(),
                                                   addressMandatoryCountryCode.getAddressLine3(),
                                                   addressMandatoryCountryCode.getAddressLine4(),
                                                   addressMandatoryCountryCode.getAddressLine5(),
                                                   addressMandatoryCountryCode.getAddressLine6(),
                                                   addressMandatoryCountryCode.getPostCode(),
                                                   addressMandatoryCountryCode.getCountryCode()
                                                    );

        addressDao.persist(merchantAddress);
        
        Merchant existingMerchant = merchantDao.getMerchantByIdentification(merchantInfoType.getMerchantId());
        if(null == existingMerchant){
        	final Merchant merchant = new Merchant(merchantAddress.getId(),
                    metaData.getApMessageId(),
                    merchantInfoType.getMerchantId(),
                    merchantInfoType.getMerchantCmnName(),
                    merchantInfoType.getMerchantName(),
                    AccountNumber.valueOf(merchantInfoType.getCreditAct().getCreditActNum()),
                    SortCode.valueOf(merchantInfoType.getCreditAct().getCreditSortCode()),
                    distributor.getId(),
                    merchantInfoType.getMerchantCtgyCd(),
                    ActivationStatus.ACTIVE);
				merchant.setCreditRollNumber(null == merchantInfoType.getCreditAct().getRollNumber() ? null : RollNumber.valueOf(merchantInfoType.getCreditAct().getRollNumber()));
				merchant.setUpdateReason(merchantInfoType.getUpdateReason());
				merchant.setMerchantGroupId(merchantInfoType.getMerchantGroupId());
				if(null != merchantInfoType.getMerchantLogo()){
				merchant.setMerchantLogo(new ByteArrayInputStream(merchantInfoType.getMerchantLogo()),merchantInfoType.getMerchantLogo().length, merchantDao.getSession());
				}
				merchantDao.persist(merchant);
        }else {
        	existingMerchant.setAddressId(merchantAddress.getId());
        	existingMerchant.setApMessageId(metaData.getApMessageId());
        	existingMerchant.setMerchantIdentification(merchantInfoType.getMerchantId());
        	existingMerchant.setCommonName(merchantInfoType.getMerchantCmnName());
        	existingMerchant.setName(merchantInfoType.getMerchantName());
        	existingMerchant.setCreditAccountNumber(AccountNumber.valueOf(merchantInfoType.getCreditAct().getCreditActNum()));
        	existingMerchant.setCreditSortCode(SortCode.valueOf(merchantInfoType.getCreditAct().getCreditSortCode()));
        	existingMerchant.setDistributorId(distributor.getId());
        	existingMerchant.setMerchantCtgyCode(merchantInfoType.getMerchantCtgyCd());
            existingMerchant.setActivationStatus(ActivationStatus.ACTIVE);
        	existingMerchant.setCreditRollNumber(null == merchantInfoType.getCreditAct().getRollNumber() ? null : RollNumber.valueOf(merchantInfoType.getCreditAct().getRollNumber()));
        	existingMerchant.setUpdateReason(merchantInfoType.getUpdateReason());
        	existingMerchant.setMerchantGroupId(merchantInfoType.getMerchantGroupId());
			if(null != merchantInfoType.getMerchantLogo()){
				existingMerchant.setMerchantLogo(new ByteArrayInputStream(merchantInfoType.getMerchantLogo()),merchantInfoType.getMerchantLogo().length, merchantDao.getSession());
			}
			//merchantDao.persist(existingMerchant);
        }
        
    }

    public void setMerchantDao(MerchantDao merchantDao) {
        this.merchantDao = merchantDao;
    }

    public void setAddressDao(AddressDao addressDao) {
        this.addressDao = addressDao;
    }

    public void setDistributorDao(DistributorDao distributorDao) {
        this.distributorDao = distributorDao;
    }
}